
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'package:my_flu01/page/user.dart';
import 'package:remixicon/remixicon.dart';
import 'package:flutter_zoom_drawer/flutter_zoom_drawer.dart';
import 'homePage.dart';
import 'member/index.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';


///支持抽屉效果的首页
class IndexCreenContent extends StatefulWidget {
  const IndexCreenContent({Key? key}) : super(key: key);

  @override
  State<IndexCreenContent> createState() => _IndexPageState();


}

//如果有多个AnimationController，就是用TickerProviderStateMixin
class _IndexPageState extends State<IndexCreenContent>  with TickerProviderStateMixin {
  /// 当前页
  late int _currentPage = 0;
  int _selectedIndex = 0;

  /// 页面
  final List<Widget> _pages = [
    const HomePage(text: "text", map: {"name": "laich"}),
    const User(),
    //HomePage2(),
    const MyIndexPage(),
    const MyIndexPage(),
  ];

  /// Tab 控制
  late final TabController _pageController = TabController(
    initialIndex: _currentPage,
    length: _pages.length,
    vsync: this,
  );




  /// PageView 控制
  final PageController _pageViewController = PageController();

  void _onItemTapped(index) {
    if (index == 2) {
      int curPage = _pageViewController.page?.round() ?? 0;
      setState(() {
        _selectedIndex = curPage > 1 ? (curPage + 1) : curPage;
      });
      //app.route('/feedback');
    } else {
      index = index > 1 ? (index - 1) : index;
      setState(() {
        _selectedIndex = index;
        _pageViewController.animateToPage(
          _selectedIndex,
          duration: const Duration(milliseconds: 300),
          curve: Curves.easeIn,
        );
      });
    }
  }



  /// 进步按钮动画
  late AnimationController _stepButtonController;
  late Animation<double> _stepButtonAnimation;
  late CurvedAnimation _stepButtonCurve;

  /// Tab icon大小
  final double _tabIconSize = 20.sp;

  @override
  void initState() {
    super.initState();

    /// 进步按钮Icon动画
    _stepButtonController = AnimationController(
      duration: const Duration(milliseconds: 500),
      vsync: this,
    );
    _stepButtonCurve = CurvedAnimation(
      parent: _stepButtonController,
      curve: Curves.fastOutSlowIn,
    );
    _stepButtonAnimation =
        Tween(begin: 0.0, end: 1.0).animate(_stepButtonController);
    _stepButtonAnimation.addListener(() {
      setState(() {});
    });
  }

  @override
  void dispose() {
    /// 进步按钮Icon动画
    _stepButtonController.dispose();

    /// Tab控制
    _pageController.dispose();

    /// PageView控制
    _pageViewController.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final ThemeData appTheme = Theme.of(context);

    return Scaffold(
        body: PageView( //PageView可用于Widget的整屏滑动切换，如当代常用的短视频APP中的上下滑动切换的功能，也可用于横向页面的切换，如APP第一次安装时的引导页面，也可用于开发轮播图功能
          controller: _pageViewController,
          physics: const NeverScrollableScrollPhysics(),//ScrollPhysics 的作用是 确定可滚动控件的物理特性，
          children: _pages,
        ),
        bottomNavigationBar:DecoratedBox(
            decoration: BoxDecoration(
            color:
            appTheme.bottomNavigationBarTheme.backgroundColor ?? Colors.blueAccent,
            boxShadow: [
            BoxShadow(color: Colors.black.withOpacity(0.04), blurRadius: 24),
            ],
        ),
          child:  SafeArea(
          child: Stack(
          alignment: Alignment.centerLeft,
          children: [
            /// 菜单
            TabBar(
              enableFeedback: true,
              padding: EdgeInsets.only(left: 40.w),
              controller: _pageController,
              indicatorColor: Colors.transparent,
              labelStyle: TextStyle(
                height: 0.5.h,
                fontSize: 10.sp,
                fontWeight: FontWeight.bold,
                color: Colors.black,
              ),
              unselectedLabelStyle: TextStyle(
                height: 0.5.h,
                fontSize: 10.sp,
                fontWeight: FontWeight.bold,
              ),
              tabs: [
                Tab(
                  key: const Key('tab_home'),
                  text: "首页",
                  icon: Icon(
                    CupertinoIcons.house,
                    size: _tabIconSize,
                  ),
                ),
                Tab(
                  key: const Key('tab_mood'),
                  text: "次页",
                  icon: Icon(
                    Remix.heart_3_line,
                    size: _tabIconSize,
                  ),
                ),
                Tab(
                  key: const Key('tab_statistic'),
                  text: "导航",
                  icon: Icon(
                    Remix.bar_chart_line,
                    size: _tabIconSize,
                  ),
                ),
                Tab(
                  key: const Key('tab_my'),
                  text: "我的",
                  icon: Icon(
                    CupertinoIcons.person_alt_circle,
                    size: _tabIconSize,
                  ),
                ),
              ],
              onTap: (value) {
                switch (value) {
                  case 2:
                  // 统计菜单触发
                   // statistic.init(context);
                }
                _pageViewController.jumpToPage(value);
                setState(() {
                  _currentPage = value;
                });
              },
            ),

            /// 侧栏  Semantics，描述一个特定的控件,分屏，搜索等
            Semantics(
              button: true,
              label: '打开设置',
              child: GestureDetector( //Gesture：手势Detector：识别）组件
                key: const Key('tab_screen_left'),
                child: Container(
                  width: 36.w,
                  height: 42.w,
                  decoration: BoxDecoration(
                    color: Colors.black12,
                    borderRadius: BorderRadius.only(
                      topRight: Radius.circular(14.sp),
                      bottomRight: Radius.circular(14.sp),
                    ),
                  ),
                  child: ValueListenableBuilder<DrawerState>(
                    valueListenable: ZoomDrawer.of(context)!.stateNotifier,
                    builder: (_, state, child) {
                      if (state == DrawerState.closed) {
                        _stepButtonController.reverse();
                      } else {
                        _stepButtonController.forward();
                      }
                      return AnimatedBuilder(
                        animation: _stepButtonAnimation,
                        builder: (context, child) => Transform.rotate(  //Transform.rotate可以对子组件进行旋转变换
                          angle: _stepButtonCurve.value * 3.14,
                          child: child,
                        ),
                        child: Icon(
                          Remix.arrow_right_line,
                          size: 14.sp,
                          color: const Color(0xFFEFEFEF),
                        ),
                      );
                    },
                  ),
                ),
                onTap: () {
                  /// 侧栏操作 https://blog.csdn.net/weixin_45628777/article/details/129644324
                  //vibrate();
                  ZoomDrawer.of(context)?.toggle.call(); //flutter_zoom_drawer实现带动画效果的抽屉
                },
              ),
            ),
          ],

          ),
        )
      // 使用公用的底部框
        )
    );
  }


  Widget renderLoading() {
    return Container(
      color: Colors.white,
      child: const Center(
        child: CircularProgressIndicator(),
      ),
    );
  }
}
